package com.blog.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.blog.admin.pojo.auth.Permission;
import com.blog.admin.service.PermissionService;
import com.blog.admin.utils.ResultVo;
import com.blog.admin.utils.UserUtils;

import cn.hutool.core.util.StrUtil;

@Controller
@RequestMapping(value = "/admin/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresPermissions("permission:list")
	@ResponseBody
	public JSONObject submitLogin() {
		List<Permission> permission = permissionService.selectAll();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", 0);
		jsonObject.put("count", permission.size());
		jsonObject.put("msg", "全部权限列表");
		jsonObject.put("data", permission);
		return jsonObject;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@RequiresPermissions("permission:delete")
	@ResponseBody
	public boolean deletePermission(String id) {
		return permissionService.deletePermission(id);
	}
	
	//type 0添加目录  1添加菜单或按钮  2修改菜单或按钮
	// userType 0目录  1、菜单   2、按钮
	@RequestMapping(value = "/addPermissionPage", method = RequestMethod.GET)
	public String addpermissionPage(HttpServletRequest request) {
		String id = request.getParameter("id");
		String opt = request.getParameter("opt");
		String type = request.getParameter("type");
		Permission permission = new Permission();
		if (StrUtil.isNotBlank(id) && "edit".equals(opt)) {
			permission = permissionService.getPermissionById(Integer.valueOf(id));
			request.setAttribute("parentId", permission.getParentId());
		}else {//添加菜单或者按钮
			if ("0".equals(type)) { //添加目录  父级id为0
				request.setAttribute("parentId", 0);
			}else{ //添加子菜单或按钮  父级id为 当前id
				request.setAttribute("parentId", Integer.valueOf(id));
			}
		}
		request.setAttribute("type", type);
		request.setAttribute("permission", permission);
		return "pages/sysManager/addpermissionPage";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("permission:save")
	@ResponseBody
	public JSONObject save(@Valid Permission permission, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			for (ObjectError error : bindingResult.getAllErrors()) {
				return ResultVo.fail(error.getDefaultMessage());
			}
		}
		if (permission.getId() != null) { // 更新操作
			permissionService.update(permission);
		} else {
			permissionService.insert(permission);
		}
		return ResultVo.success("保存成功");
	}
}
